package com.itheima.service;

import com.itheima.common.R;
import com.itheima.mapper.DishMapper;
import com.itheima.mapper.FlavorsMapper;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishPage;
import com.itheima.pojo.Flavors;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author Envy
 */
@Service
public class DishService {
    @Resource
    private DishMapper dishMapper;
    @Resource
    private FlavorsMapper flavorsMapper;

    /**
     * 菜品分页查询
     */
    public R<DishPage> pageQuery(String name, Integer page, Integer pageSize) {

        List<DishPage> dishPages = dishMapper.pageQuery(name, page, pageSize);

        DishPage dp = new DishPage();
        //设置所有页面的总条数
        dp.setTotal(dishMapper.count());
        //设置每页的显示数据条数
        dp.setPageSize(pageSize);
        if (dishPages != null) {
            //设置显示当前页面
            dp.setCurrent(page);
            //把查询的所有数据放到records集合
            dp.setRecords(dishPages);
            //数据都在records集合 所以这里返回Dishpage的对象
            return R.success(dp);
        }
        return R.error("暂无数据");
    }

    /**
     * 根据id查询菜品信息
     */
    public R<Dish> selectInfo(String id) {
        Dish dish = dishMapper.selectInfo(id);
        List<Flavors> flavors = flavorsMapper.selectFlavors(dish.getId());

        if (flavors != null) {
            dish.setFlavors(flavors);
        }
        return R.success(dish);
    }

    /**
     * 菜品下拉列表
     */

    public List<Dish> pullDown() {
        return dishMapper.pullDown();
    }

    /**
     * 新增菜品
     */
    public R<Dish> addDish(Dish dish) {
        //获取时间戳,将时间戳作为id
        long timeMillis = System.currentTimeMillis();
        String id = timeMillis + "";
        dish.setId(id);

        List<Flavors> flavors2 = dish.getFlavors();
        dishMapper.addDish(dish);
        String id1 = dish.getId();
        if (flavors2.size() != 0) {
            for (Flavors flavors : flavors2) {
                Random random = new Random();
                int nextInt = random.nextInt(99999999);
                String id2 = nextInt + "";

                flavors.setId(id2);
                flavors.setDishId(id1);
                flavorsMapper.addFlavors(flavors);
            }

            return R.success(dish);
        } else {
            return R.success(dish);
        }
    }

    /**
     * 删除菜品
     */
    public R<Object> delete(List<String> ids) {

        for (String id : ids) {
            dishMapper.deleteWithFlavor(id);
        }
        return R.success("删除成功");
    }

    /**
     * 修改菜品
     */
    public R<Dish> update(Dish dish) {
        //取出dish，根据dish的id更新dish表
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(date);
        dish.setUpdateTime(time);

        dishMapper.update(dish);


        //在dish中取出flavors集合
        List<Flavors> flavors = dish.getFlavors();
        String dishId = dish.getId();

        //根据dish的id删除flavors中所有有关dish相关id的口味条目
        flavorsMapper.deleteflavors(dishId);

        //遍历口味集合，逐一添加到flavors表中，
        for (Flavors flavors1 : flavors) {
            Random random = new Random();
            int nextInt = random.nextInt(99999999);
            String id2 = nextInt + "";

            flavors1.setId(id2);
            flavors1.setDishId(dishId);
            flavorsMapper.addFlavors(flavors1);
        }

        return R.success(dish);
    }

    /**
     * 菜品停售/起售
     */
    public R<Object> updatestatus(List<String> ids, Integer status) {
        for (String id : ids) {
            dishMapper.updatestatus(id, status);
        }
        return R.success("修改成功");
    }
}
